package com.lottemall.order.web;

import com.lottemall.order.dto.OrderDTO;
import com.lottemall.order.pojo.Order;
import com.lottemall.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;


    /**
     * 创建订单
     * http://api.lottemall.com/api/order-service/order
     *
     * @param orderDTO 封装好的订单传输对象有前台传过来的，只是一些必要标识
     * @return 订单id
     */
    @PostMapping
    public ResponseEntity<Long> createOrder(@RequestBody OrderDTO orderDTO) {
        return ResponseEntity.ok(orderService.createOrder(orderDTO));
    }

    /**
     * 查询订单信息
     * http://api.lottemall.com/api/order-service/order/1097446690517942272
     * @param id 订单id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Order> queryOrderById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(orderService.queryOrderById(id));
    }

    /**
     * 创建支付链接
     * http://api.lottemall.com/api/order-service/order/url/1097446690517942272
     * @param orderId
     * @return
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> createPayUrl(@PathVariable("id") Long orderId){
        return ResponseEntity.ok(orderService.createPayUrl(orderId));
    }

    /**
     * 查询订单支付状态（用于前台用户扫描支付二维码的定时查询判断用户是否支付成功进行跳转）
     * http://api.lottemall.com/api/order-service/order/state/1097758182387879936
     * @param orderId
     * @return
     */
    @GetMapping("/state/{id}")
    public ResponseEntity<Integer> queryOrderState(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(orderService.queryOrderState(orderId).getValue());
    }

}
